<?php
class b2c_finder_sales_applyShenhe
{
    /**
     * @var 定义方法名称的变量
     */
    public $detail_basic = '基本信息';

    public function __construct($app)
    {
        $this->app = $app;
        $this->render = $this->app->render();
		$this->mdl_order=$this->app->model('orders');
		$this->db = $this->mdl_order->db;
		// $this->erp_caller_obj = kernel::service('apioubao.rpc_erp_request');
	    $this->column_uname = app::get('b2c')->_('客户名称');
	    $this->column_clentSource = app::get('b2c')->_('业务员');
	    $this->column_totalMoney = app::get('b2c')->_('原价');
	    $this->column_zhekouMoney = app::get('b2c')->_('折扣价');
	    $this->column_zhekoupercent= app::get('b2c')->_('折扣率');
	    $this->column_itemnum= app::get('b2c')->_('数量');
    }

    /**
     * @desc
     * @author guomin
     * @date 2018/5/17  10:40
     * @param $return_id
     * @return mixed
     */
    public function detail_basic($return_id)
    {
        $obj_return_product = $this->app->model('order_apply_sale');
        $arr_return_product = $obj_return_product->dump($return_id);
        if ($arr_return_product['shenhe_status']=='1'){
            $arr_return_product['status']='已通过';
        }elseif ($arr_return_product['shenhe_status']=='2'){
            $arr_return_product['status']='已拒绝';
        }else{
            $arr_return_product['status']='未审核';
        }
        $this->render->pagedata['info'] = $arr_return_product;
        return $this->render->fetch('admin/sales/detail.html');
    }
    //操作按钮
    public $column_editbutton = '操作';
    public $column_editbutton_order = '1';

    /**
     * @desc
     * @author guomin
     * @date 2018/5/17  10:40
     * @param $row
     * @return mixed
     */
    public function column_editbutton($row)
    {
        // $render = $this->app->render();
        $order_id=$row['order_id'];
        // $mdl_order=$this->app->model('orders');
        $orders= $this->mdl_order->getRow('pay_status',array('order_id'=>$order_id));
        $arr = array(
            'app'=>$_GET['app'],
            'ctl'=>$_GET['ctl'],
            'act'=>$_GET['act'],
            'action'=>'detail',
            'finder_name'=>$_GET['_finder']['finder_id'],
            'finder_id'=>$_GET['_finder']['finder_id'],
            'finderview'=>'detail_basic',
        );
        $link = 'index.php?'.utils::http_build_query($arr).'&id='.$row['apply_sale_id'].'&_finder[finder_id]='.$_GET['_finder']['finder_id'];
        $status_accept = array(
            'id'=>'x-return-status_'.$row['apply_sale_id'].'_1',
            'href'=>"index.php?app=b2c&ctl=admin_sales_applyShenhe&act=save",
            'target'=>'request::{url:\''.$link.'&status=1\',data:\'apply_sale_id='.$row['apply_sale_id'].'&shenhe_status=1\'}',
            'comment'=>'true',
            'label'=>app::get('b2c')->_('通过'),
        );
        $status_reduce = array(
            'id'=>'x-return-status_'.$row['apply_sale_id'].'_2',
            'href'=>"index.php?app=b2c&ctl=admin_sales_applyShenhe&act=save",
            'target'=>'request::{url:\''.$link.'&status=2\',data:\'apply_sale_id='.$row['apply_sale_id'].'&shenhe_status=2\'}',
            'comment'=>'true',
            'label'=>app::get('b2c')->_('拒绝'),
        );
        $status_cancel = array(
            'id'=>'x-return-status_'.$row['apply_sale_id'].'_3',
            'href'=>"index.php?app=b2c&ctl=admin_sales_applyShenhe&act=save",
            'target'=>'request::{url:\''.$link.'&status=0\',data:\'apply_sale_id='.$row['apply_sale_id'].'&shenhe_status=0\'}',
            'comment'=>'true',
            'label'=>app::get('b2c')->_('取消审核'),
        );
        //未付款
        if ($orders['pay_status']=='0'){
            if($row['shenhe_status']==0){
                $arr_links = array(
                    $status_accept,
                    $status_reduce
                );
                $can_process_flag = true;
            }
            //取消审核
            if ($row['shenhe_status']==1){
	            $arr_links = array(
		            $status_cancel
	            );
	            $can_process_flag = true;
            }
        }
        $this->render->pagedata['arr_links'] = $arr_links;
        $this->render->pagedata['can_process'] = $can_process_flag;
        return $this->render->fetch('admin/sales/apply.html');
    }

	var $column_uname_order = 3;
	public function column_uname($row){
		$member=$this->getMember($row['order_id']);
		return $member[0]['name'];
	}
	var $column_clentSource_order = 4;
	public function column_clentSource($row){
		$member=$this->getMember($row['order_id']);
		$member_id=$member[0]['member_id'];
		//从进销存获取业务员信息
		$db_jxc = DB_JXC;
		$sql = "SELECT b.employName FROM {$db_jxc}.jichu_client as a 
				LEFT JOIN {$db_jxc}.jichu_employ as b ON b.id = a.traderId
				WHERE a.member_id = {$member_id};
		";
		$data = $this->db->select($sql);
		// $post = array(
		// 	'method'=>'jichu.getTrader',
		// 	'params'=>array(
		// 		'member_id'=>$member_id,
		// 	)
		// );
		// $result = $this->erp_caller_obj->call_api_oubao($post);
		// $r = json_decode($result,true);
		// if(!$r) {
		// 	exit;
		// }
		// if($r['rsp']===false) {
		// 	exit;
		// }
		// //开始输出相关布卷信息
		// $data = $r['data'];
		return $data['0']['employName'];
	}
	//原价
	var $column_totalMoney_order = 5;
	public function column_totalMoney($row){
		$order_id=$row['order_id'];
		$orders=$this->getDiscount($order_id,$row['shenhe_status'],$row['discount']);
		return $orders[0];
	}

	var $column_zhekouMoney_order = 5;
	public function column_zhekouMoney($row){
		$order_id=$row['order_id'];
		$orders=$this->getDiscount($order_id,$row['shenhe_status'],$row['discount']);
		return $orders[1];
	}
	var $column_zhekoupercent_order = 5;
	public function column_zhekoupercent($row){
		$order_id=$row['order_id'];
		$orders=$this->getDiscount($order_id,$row['shenhe_status'],$row['discount']);
		return $orders[2];
	}

	/**
	 * @desc 订单折扣相关信息
	 * @author guomin
	 * @date 2018/5/24  10:01
	 * @param  string $order_id
	 * @param  string $shenhe_status
	 * @param  int $discount
	 * @return array
	 */
	private function getDiscount($order_id,$shenhe_status,$discount){
		// $mdl_order=$this->app->model('orders');
		$orders= $this->mdl_order->getRow('*',array('order_id'=>$order_id));
		if ($shenhe_status==0){//未审核
			$totalMoney=$orders['total_amount'];
			$zhekouMoney=$orders['total_amount']-$discount;
		}elseif ($shenhe_status==1){//已通过
			$totalMoney=$orders['total_amount'] + $discount;
			$zhekouMoney=$orders['total_amount'];
		}elseif ($shenhe_status==2){//已拒绝
			$totalMoney=$orders['total_amount'];
			$zhekouMoney=$orders['total_amount']-$discount;
		}
		$zhekoupercent=(round($zhekouMoney/$totalMoney,2)*100).'%';
		return array($totalMoney,$zhekouMoney,$zhekoupercent);
	}

	/**
	 * @desc 获取用户名
	 * @author guomin
	 * @date 2018/5/24  10:01
	 * @param  string $order_id
	 * @return array
	 */
	private function getMember($order_id){
		$sql="SELECT m.name,m.client_source,m.member_id FROM sdb_b2c_members m INNER JOIN sdb_b2c_orders o ON o.member_id=m.member_id WHERE o.order_id='{$order_id}'LIMIT 1";
		$member = $this->db->select($sql);
		return $member;
	}

	public function column_itemnum($row){
		$order_id = $row['order_id'];
		$sql="SELECT itemnum  FROM sdb_b2c_orders  WHERE order_id='{$order_id}'";
		$data = $this->db->select($sql);
		return $data['0']['itemnum'];
	}
}